<template>
  <!-- 做添加和编辑 -->
  <div>
    <el-form ref="deptForm" label-width="120px">
      <el-form-item label="部门名称">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>

      <el-form-item label="部门编码">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>

      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDepartments, getDepartDetail,updateDepartments} from "@/api/departments"
import { getEmployeeSimple } from "@/api/employees";
export default {
    props:{
        id:{
            type:String,
            required:true
        },
        isEdit:{
            type:Boolean,
            required:true
        }
    },
  data() {
    return {
      employees: [],
      form: {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "", // 部门介绍
        pid:""
      },
    };
  },
  created() {
    this.loadEmployees();
    this.loadDetail()
  },
  methods: {
//   获取当前部门详情
    async loadDetail(){
        if(this.isEdit){
            // alert('现在编辑')
            const res = await getDepartDetail(this.id)
            console.log('获取编辑数据',res);
            this.form = res.data
        }
      },
    async loadEmployees() {
      try {
        const res = await getEmployeeSimple();
        console.log("获取员工信息", res);
        this.employees = res.data;
        // console.log(this.employees);
      } catch (err) {
        console.log(err);
      }
    },
    async doAdd(){
    const res = await addDepartments({...this.form,pid:this.id})
    console.log(res);
        this.$emit('update-depart')
    },
     async doEdit(){
           try {
                await updateDepartments(this.form)
                this.$message({ type: 'success', message: '修改成功' })
                this.$emit('update-depart')
            } catch (err) {
                console.log(err)
                this.$message({ type: 'error', message: err })
          }
    },
    hSubmit() {
      // 先省略表单验证
      // 根据 isEdit的值，决定做保存还是添加
      // if (this.isEdit) {
      //   this.doEdit()
      // } else {
      //   this.doAdd()
      // }
      this.isEdit ? this.doEdit() : this.doAdd()
    },
     hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    }
  },
};
</script>

<style>
</style>